package com.diyizu.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diyizu.youdianda.common.PageResult;
import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.dto.YddArticleDto;
import com.diyizu.youdianda.entity.SysUser;
import com.diyizu.youdianda.entity.YddArticle;
import com.diyizu.youdianda.entity.YddCate;
import com.diyizu.youdianda.service.YddArticleService;
import com.diyizu.youdianda.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddCateService yddCateService;

    @GetMapping("/list")
    public R list(@RequestParam Map map) {
        String start_time = (String) map.get("start_time");
        String end_time = (String) map.get("end_time");
        String title = (String) map.get("title");
        String cateid = (String) map.get("cateid");
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");

        //分页构造器
        Page<YddArticle> yddArticlePage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        Page<YddArticleDto> articleDtoPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();

        if (Strings.isNotEmpty(start_time)) {
            lqw.between(YddArticle::getCreateDate, Integer.parseInt(start_time), Integer.parseInt(end_time));
        }
        lqw.like(title != "", YddArticle::getTitle, title);
        lqw.eq(cateid != "", YddArticle::getCateid, cateid);
        yddArticleService.page(yddArticlePage, lqw);

        //复制
        BeanUtils.copyProperties(yddArticlePage, articleDtoPage, "records");
        List<YddArticle> records = yddArticlePage.getRecords();

        List<YddArticleDto> collect = records.stream().map((item) -> {
            YddArticleDto yddArticleDto = new YddArticleDto();
            BeanUtils.copyProperties(item, yddArticleDto);
            Integer itemCateid = item.getCateid();
            YddCate yddCate = yddCateService.getById(itemCateid);
            yddArticleDto.setCatename(yddCate.getCatename());
            return yddArticleDto;
        }).collect(Collectors.toList());
        articleDtoPage.setRecords(collect);


        PageResult pageResult = new PageResult();
        pageResult.setData(articleDtoPage.getRecords());
        pageResult.setCount(articleDtoPage.getTotal());
        pageResult.setTotalPages((long) Math.ceil((double) articleDtoPage.getTotal() / yddArticlePage.getSize()));
        pageResult.setPageSize(articleDtoPage.getSize());
        pageResult.setCurrentPage(articleDtoPage.getCurrent());
        return R.success(pageResult);
    }

    @GetMapping("/del")
    public R del(int id) {
        yddArticleService.removeById(id);
        return R.success("删除成功");
    }

    @PostMapping("/add")
    public R add(@RequestBody YddArticle yddArticle) {
        yddArticleService.save(yddArticle);
        return R.success("成功");
    }

    @GetMapping("/rowInfo")
    public R rowInfo(int id) {
        YddArticle byId = yddArticleService.getById(id);
        return R.success(byId);
    }

    @PostMapping("/update")
    public R update(@RequestBody YddArticle yddArticle) {
        yddArticle.setUpdateDate(new Date().getTime());
        yddArticleService.updateById(yddArticle);
        return R.success("修改成功");
    }
}
